package com.ruoyi.project.common.component;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.Bucket;
import com.aliyun.oss.model.PutObjectResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @ClassName AliOSSComponent
 * @Description
 * @Author Small
 * @Date 2020/8/19 18:25
 * @Version 1.0
 **/

@Component("aliOSSComponent")
public class AliOSSComponent {
    @Autowired
    private AliOssProperties aliOssProperties;


    /**
     * 文件流的方式上传文件
     *
     */
    public String upload(MultipartFile file) {
        // 文件类型
        String extension = FileUploadUtils.getExtension(file);
        String fileName = UUID.randomUUID().toString().replaceAll("-", "") + "." + extension;
        ByteArrayOutputStream bos = null;
        ByteArrayInputStream swapStream = null;
        try {
            // 通过流形式处理图片
            bos = new ByteArrayOutputStream();
            Thumbnails.of(file.getInputStream()).scale(0.3f).outputQuality(0.25f).toOutputStream(bos);
            swapStream = new ByteArrayInputStream(bos.toByteArray());
            boolean upload = upload(aliOssProperties.getBucket(), fileName, swapStream);
            return fileName;

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != swapStream) {
                try {
                    swapStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != bos) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


        return null;
    }


    public String upload(String extension, InputStream inputStream) {

        String fileName = UUID.randomUUID().toString().replaceAll("-", "") + "." + extension;
        try {
            boolean upload = upload(aliOssProperties.getBucket(), fileName, inputStream);
            if (upload) {
                return fileName;
            }
        } catch (OSSException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 文件流的方式上传文件
     *
     * @param bucketName  存储空间
     * @param key  文件标识
     * @param is   文件流
     */
    public boolean upload(String bucketName, String key, InputStream is) {
        OSS ossClient = null;
        try {
            ossClient = initClient();
            PutObjectResult putObjectResult = ossClient.putObject(bucketName, key, is);
            return true;
        } catch (OSSException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } finally {
            // 关闭OSSClient。
            if (null != ossClient) {
                ossClient.shutdown();
            }
        }
        return false;
    }


    /**
     * 删除图片
     *
     * @param bucketName  存储空间
     * @param objectName  文件名
     * @return
     */
    public boolean delete(String bucketName, String objectName) {
        OSS ossClient = null;
        try {
            ossClient = initClient();
            ossClient.deleteObject(bucketName, objectName);
            return true;
        } catch (OSSException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } finally {
            // 关闭OSSClient。
            if (null != ossClient) {
                ossClient.shutdown();
            }
        }
        return false;
    }

    /**
     * 删除图片
     *
     * @param objectName  文件名
     * @return
     */
    public boolean delete(String objectName) {
        if (StringUtils.isNotEmpty(objectName)) {
            OSS ossClient = null;
            try {
                ossClient = initClient();
                ossClient.deleteObject(aliOssProperties.getBucket(), objectName);
                return true;
            } catch (OSSException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            } finally {
                // 关闭OSSClient。
                if (null != ossClient) {
                    ossClient.shutdown();
                }
            }
            return false;
        }
        return true;
    }

    /**
     * 创建存储空间
     *
     * @param bucketName
     */
    public Bucket createBucket(String bucketName) {
        OSS ossClient = null;
        try {
            // 创建OSSClient实例。
            ossClient = initClient();
            // 创建存储空间。
            return ossClient.createBucket(bucketName);
        } catch (OSSException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } finally {
            // 关闭OSSClient。
            if (null != ossClient) {
                ossClient.shutdown();
            }
        }
        return null;
    }


    private OSS initClient() {
        return new OSSClientBuilder().build(aliOssProperties.getEndpoint(), aliOssProperties.getAccessKeyId(), aliOssProperties.getAccessKeySecret());
    }

}
